package third.lib.zfb.login.app;

import com.alibaba.fastjson.JSON;
import com.alipay.api.AlipayApiException;
import com.alipay.api.AlipayClient;
import com.alipay.api.request.AlipaySystemOauthTokenRequest;
import com.alipay.api.request.AlipayUserInfoShareRequest;
import com.alipay.api.response.AlipaySystemOauthTokenResponse;
import com.alipay.api.response.AlipayUserInfoShareResponse;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import third.lib.common.exception.ThirdException;
import third.lib.common.logger.ThirdLogger;
import third.lib.common.status.ThirdStatus;
import third.lib.zfb.common.config.ZfbAppConfig;
import third.lib.zfb.common.utils.ZfbUtils;
import third.lib.zfb.login.app.model.ZfbAppToken;
import third.lib.zfb.login.app.model.ZfbAppUserInfo;

@Service
public class ZfbAppLogin {
    private final Logger logger = LoggerFactory.getLogger(ZfbAppLogin.class);
    @Autowired
    ZfbAppConfig config;
    @Autowired
    ThirdLogger thirdLogger;

    public ZfbAppLogin() {
    }

    public ZfbAppToken getToken(String code) throws ThirdException {
        thirdLogger.info("getToken： " + JSON.toJSONString(this.config));
        AlipaySystemOauthTokenResponse response = null;

        try {
            AlipayClient alipayClient = ZfbUtils.getRequest(this.config.getAppId(), this.config.getPriKey(), this.config.getPubAppCrtPath(), this.config.getPubAliCrtPath(), this.config.getPubRootCrtPath());
            AlipaySystemOauthTokenRequest request = new AlipaySystemOauthTokenRequest();
            request.setGrantType("authorization_code");
            request.setCode(code);
            response = (AlipaySystemOauthTokenResponse)alipayClient.certificateExecute(request);
        } catch (AlipayApiException ex) {
            ex.printStackTrace();
            throw new ThirdException(ThirdStatus.NET);
        } catch (ThirdException ex1) {
            throw ex1;
        }

        if (!response.isSuccess()) {
//            this.debugPrint.debug("支付宝登陆返回: " + JSON.toJSONString(response));
            throw new ThirdException(ThirdStatus.ZFB);
        } else {
            return new ZfbAppToken(response.getUserId(), response.getAccessToken());
        }
    }

    public ZfbAppUserInfo getUserInfo(String accessToken) throws ThirdException {
        thirdLogger.info("getUserInfo： " + JSON.toJSONString(this.config));
        AlipayUserInfoShareResponse response = null;

        try {
            AlipayClient alipayClient = ZfbUtils.getRequest(this.config.getAppId(), this.config.getPriKey(), this.config.getPubAppCrtPath(), this.config.getPubAliCrtPath(), this.config.getPubRootCrtPath());
            AlipayUserInfoShareRequest request = new AlipayUserInfoShareRequest();
            response = (AlipayUserInfoShareResponse)alipayClient.certificateExecute(request, accessToken);
        } catch (AlipayApiException var5) {
            var5.printStackTrace();
            throw new ThirdException(ThirdStatus.NET);
        } catch (ThirdException ex) {
            throw ex;
        }

        if (response.isSuccess()) {
            int sex = response.getGender() == "F" ? 2 : 1;
            ZfbAppUserInfo userInfo = new ZfbAppUserInfo(response.getUserId(), response.getCity(), response.getNickName(), response.getAvatar(), response.getProvince(), sex);
            return userInfo;
        } else {
            System.err.println("支付宝请求userInfo: " + JSON.toJSONString(response));
            throw new ThirdException(ThirdStatus.ZFB);
        }
    }

    public String getAuthInfo() throws ThirdException {
        thirdLogger.info("获取登录权限字符串: " + JSON.toJSONString(this.config));
        if (this.config.getAppId() != null && !this.config.getAppId().isEmpty()) {
            if (this.config.getPid() != null && !this.config.getPid().isEmpty()) {
                if (this.config.getPriKey() != null && !this.config.getPriKey().isEmpty()) {
                    return ZfbUtils.getAuthInfo(this.config.getPid(), this.config.getAppId(), this.config.getPriKey());
                } else {
                    throw new ThirdException(ThirdStatus.ZFB);
                }
            } else {
                throw new ThirdException(ThirdStatus.ZFB);
            }
        } else {
            throw new ThirdException(ThirdStatus.ZFB);
        }
    }
}
